package in.ac.dei.edrp.cms.daoimpl.activitymaster;

import java.util.List;

import in.ac.dei.edrp.cms.constants.CRConstant;
import in.ac.dei.edrp.cms.daoimpl.registration.prestagingdaoimpl.TransfertNORInPSTFunction;
import in.ac.dei.edrp.cms.domain.activitymaster.StartActivityBean;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.support.TransactionTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;

public class UpdateActivityAndProcessMaster{


	static final Logger logger = Logger.getLogger(UpdateActivityAndProcessMaster.class);
	
	protected SqlMapClient sqlMapClient = null;
	
    public void setSqlMapClient(SqlMapClient sqlMapClient) {
        this.sqlMapClient = sqlMapClient;
    }
	
	
    TransactionTemplate transactionTemplate=null;

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}
	
	public UpdateActivityAndProcessMaster(SqlMapClient sqlMapClient,TransactionTemplate transactionTemplate){
		this.sqlMapClient=sqlMapClient;
		this.transactionTemplate=transactionTemplate;
	}
	/**
	 * It updates process status and end date
	 * @param startActivityBean
	 */
	public boolean updateActivityStatus(StartActivityBean startActivityBean){

		
		int maxNumber=getMaximumActivitySequence(startActivityBean);
		
		System.out.println(maxNumber+"maximum activity number ");
		
		try{
		//if activitySequence is 1 and only 1 activity is there and it is completed, then process end date 
		//and activity date and process status will be updated and has more activity and it is completed, only activity end date will be updated
		if(startActivityBean.getActivitySequence()==1 ){
			if(startActivityBean.getActivitySequence()==maxNumber){
					if(startActivityBean.getActivityStatus().equalsIgnoreCase(CRConstant.COMPLETE_STATUS)){
						sqlMapClient.update("startActivity.updateProcessEndDate", startActivityBean);
						startActivityBean.setProcessStatus(CRConstant.COMPLETE_STATUS);
						sqlMapClient.update("startActivity.updateProcessStatus", startActivityBean);
						sqlMapClient.update("startActivity.updateActivityEndDate", startActivityBean);
						}
			}
			else{
					if(startActivityBean.getActivityStatus().equalsIgnoreCase(CRConstant.COMPLETE_STATUS)){
					sqlMapClient.update("startActivity.updateActivityEndDate", startActivityBean);
					}//if complete status ends
			}
		}//if activitySwquence=1 ends
		//if activitySequence is not 1, then if it is maximum, process and activity end date with status will be updated
		//if it is not maximum, then only activity date will be updated
		else{
			if(startActivityBean.getActivitySequence()==maxNumber){
					if(startActivityBean.getActivityStatus().equalsIgnoreCase(CRConstant.COMPLETE_STATUS)){
						sqlMapClient.update("startActivity.updateProcessEndDate", startActivityBean);
						sqlMapClient.update("startActivity.updateProcessStatus", startActivityBean);
						sqlMapClient.update("startActivity.updateActivityEndDate", startActivityBean);
									
					}//if status is completed
									
			}//if activity sequence is max number
		
			//activity staus will be updated in all conditions either by ERR or COM
		}//else ends of activity sequence!=1

		startActivityBean.setActivityStatus(startActivityBean.getActivityStatus());
		sqlMapClient.update("startActivity.updateActivitySatus", startActivityBean);
		return true;
		}catch(Exception e){
			System.out.println("Exception is:"+e.getMessage());
			return false;
		}
		
	
}

	/**
	 * It returns max activitysequence for given input
	 * @param startActivityBean
	 * @return
	 */
	private int getMaximumActivitySequence(StartActivityBean startActivityBean) {
		// TODO Auto-generated method stub
		int activitySequence=0;
		try{
			List<StartActivityBean> list=sqlMapClient.queryForList("startActivity.getMaximumActivitySequence",startActivityBean);
			
			for(StartActivityBean maxValue:list){
				
				activitySequence=maxValue.getActivitySequence();
			}
		}catch(Exception e){
			System.out.println("Exception is:"+e.getMessage());
		}
		
		return activitySequence;
	}
}